import { Request, Response } from 'express';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};


export default {
  'POST /admin/login': async (req: Request, res: Response) => {
    const { password, username } = req.body;
    await waitTime(1000)
    if (password === 'admin' && username === 'admin') {
      res.send({
        code: 200,
        data: { token: 'admin' },
      });
    } else if (password === 'admin' && username === 'user') {
      res.send({
        code: 200,
        data: { token: 'user' },
      });
    } else {
      res.send({
        code: 201,
        msg: '用户名或密码错误',
      });
    }
  },

  'POST /api/user/register': async (req: Request, res: Response) => {
    await waitTime(1000)
    res.send({
      code: 200,
      data: '',
    });
  },

  'GET /api/currentUser': (req: Request, res: Response) => {
    const { authorization } = req.headers
    if (authorization === 'admin') {
      res.send({
        code: 200,
        data: {
          name: 'admin',
          avatar: '',
          path: ['/home', '/userMange', '/recyclable', '/kitchenWaste', '/harmful', '/other', '/contentMange']
        },
      });
    } else if (authorization === 'user') {
      res.send({
        code: 200,
        data: {
          name: 'user',
          avatar: '',
          path: ['/home']
        },
      });
    } else {
      res.status(401)
      res.send({
        code: 401,
        msg: '身份验证失败'
      });
    }
  },
};
